package ui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;



public class RubberBandDrawingPanel1 extends JPanel {
    private List<Shape> shapes;
    private Point startPoint;
    private Point currentPoint;
    private boolean isDrawing;
    private ShapeMode shapeMode = ShapeMode.CIRCLE;
    
    public RubberBandDrawingPanel1() {
        shapes = new ArrayList<Shape>();
        
        isDrawing = false;
        
        // 鼠标按下事件 - 开始画形状
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e)) {
                    startPoint = e.getPoint();
                    currentPoint = startPoint;
                    isDrawing = true;
                }
            }
            
            @Override
            public void mouseReleased(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e) && isDrawing) {
                    currentPoint = e.getPoint();
                    if (shapeMode == ShapeMode.CIRCLE) {
                        int radius = (int) startPoint.distance(currentPoint);
                        shapes.add(new Circle(startPoint, radius));
                    } else {
                        int width = Math.abs(currentPoint.x - startPoint.x);
                        int height = Math.abs(currentPoint.y - startPoint.y);
                        shapes.add(new Square(startPoint, width, height));
                    }
                    isDrawing = false;
                    repaint();
                }
            }
        });
        
        // 鼠标移动事件 - 更新橡皮筋线
        this.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if (isDrawing) {
                    currentPoint = e.getPoint();
                    repaint();
                }
            }
        });
        
        this.setBackground(Color.WHITE);
        this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    }
    
    public void setShapeMode(ShapeMode mode) {
        this.shapeMode = mode;
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        // 绘制所有已完成的形状
        g2d.setColor(new Color(0, 0, 100, 150)); // 半透明蓝色
        for (Shape shape : shapes) {
            if (shape instanceof Circle) {
                g2d.setColor(new Color(0, 0, 100, 150)); // 半透明蓝色

            	  
                Circle circle = (Circle) shape;
                drawCircle(g2d, circle.center, circle.radius);
            } else if (shape instanceof Square) {
                g2d.setColor(new Color(0, 100, 100, 150)); // 半透明蓝色
                Square square = (Square) shape;
                drawSquare(g2d, square.startPoint, square.width, square.height);
            }
        }
        
        // 绘制当前正在拖动的橡皮筋线和形状预览
        if (isDrawing && startPoint != null && currentPoint != null) {
            g2d.setColor(Color.RED);
            g2d.drawLine(startPoint.x, startPoint.y, currentPoint.x, currentPoint.y);
            
            if (shapeMode == ShapeMode.CIRCLE) {
                int radius = (int) startPoint.distance(currentPoint);
                g2d.setColor(new Color(255, 100, 0, 100));
                drawCircle(g2d, startPoint, radius);
            } else {
                int width = currentPoint.x - startPoint.x;
                int height = currentPoint.y - startPoint.y;
                g2d.setColor(new Color(255, 100, 0, 100));
                drawSquare(g2d, startPoint, width, height);
            }
        }
    }
    
    private void drawCircle(Graphics2D g2d, Point center, int radius) {
        g2d.fillOval(center.x - radius, center.y - radius, radius * 2, radius * 2);
    }
    
    private void drawSquare(Graphics2D g2d, Point start, int width, int height) {
        // 确保宽度和高度为正数
        int x = width < 0 ? start.x + width : start.x;
        int y = height < 0 ? start.y + height : start.y;
        int absWidth = Math.abs(width);
        int absHeight = Math.abs(height);
        g2d.fillRect(x, y, absWidth, absHeight);
    }


	public void saveShapesToFile(File file) throws Exception{
		OutputStream out = new FileOutputStream(file);
		
		ObjectOutputStream os = new ObjectOutputStream(out);
		
		os.writeObject(shapes);
		
		
	}

	public void loadShapesFromFile (File file) throws Exception{
		InputStream out = new FileInputStream(file);
		
		ObjectInputStream os = new ObjectInputStream(out);
		shapes = (List<Shape>) os.readObject();
		repaint();
	}
}
